using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Drawing;
using System.Collections;

namespace common
{
    public class Consts
    {
        public const string constHashFileName  = "hash.txt";
        public const string constCommentMarker = "#";
        public const string constRootXMLElement = "Configuration";

        public const string constNotAvailable = "N/A";
        public const string SQL_CMD_ALL_MARKER = "%";

        public const string constVersionLIC = "License version 1.0";
        public const string constVersionHWD = "Hardware Version 1.0";

        //Null
        public const decimal constNullDecimal = decimal.MinValue;
        public const int constNullInt = int.MinValue;
        public static readonly DateTime constNullDate = DateTime.MinValue;

        //Number of character use in hashing
        public const int constCharOfHashDirNo = 2;

        //Common key char
        public static string constCRLF = Environment.NewLine; //  "\r\n";
        //Common key char
        public const char constEnter = (char)13;
        public const char constTab = (char)9;
        public const char constSpace = (char)32;
        //===============================================
        //Hotkeys 
        //===============================================
        public const Keys constHotKeyLock = Keys.Space; //Control + this key

        public const Keys constHotKeyAddNew = Keys.F3;
        public const Keys constHotKeySave = Keys.F2;
        public const Keys constHotKeyEdit = Keys.F4;
        public const Keys constHotKeyAddLine = Keys.F5;
        public const Keys constHotKeyDeleteLine = Keys.F8;
        public const Keys constHotKeyViewSize = Keys.F9;

        //Microsoft system : http://support.microsoft.com/kb/320584
        public const int WM_KEYDOWN = 0x100;
        public const int WM_SYSKEYDOWN = 0x104;

    }
    public static class Settings
    {
        private static int _sysIsDesignModeCode = -1;
        public static bool sysIsDesignMode
        {
            get
            {
                if (_sysIsDesignModeCode<0)
                    _sysIsDesignModeCode = (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv"?1:0);
                return (_sysIsDesignModeCode ==1 ? true : false);
            }
        }

        public static bool sysDebugMode = true;

        // The decimal number 2.000 can be interpreted differntly in diffentt cultures, in US it is 2000 but int VN it is only 2
        // So to correctly retrieve data, it must be interpreted and store in the same culture.
        // myDataCulture specifies the "comon" culture.
        public readonly static CultureInfo sysDataCulture = new CultureInfo("en-US");

        //Common color
        public static Color sysColorDisableBG = Color.LightYellow;
        public static Color sysColorDisableFG = Color.DarkRed;
        public static Color sysColorNormalBG = Color.White;
        public static Color sysColorNormalFG = Color.Black;

        public static Color sysColorHiLightBG1 = Color.LightPink;
        public static Color sysColorHiLightFG1 = Color.Red;

        public static Color sysColorHiLightBG2 = Color.Bisque;
        public static Color sysColorHiLightFG2 = Color.Navy;

        public static Color sysColorHiLightBG3 = Color.CornflowerBlue;
        public static Color sysColorHiLightFG3 = Color.Red;

        public static Color sysColorHiLightBG4 = Color.LightSalmon;
        public static Color sysColorHiLightFG4 = Color.Red;

        public static Color sysColorHiLightBG5 = Color.MistyRose;
        public static Color sysColorHiLightFG5 = Color.Red;


        // Multi-value field is stored in the format: <prefix><value><postfix><separator>
        public static string sysListSeparatorPrefix = "/";
        public static string sysListSeparatorPostfix = "/";
        public static string sysListSeparator = " ";

        public static char[] sysSeparatorList = new char[] {','};
        public static char[] sysSeparatorsWord = new char[] { ' ', '-', ',' };
        public static char[] sysSeparatorValue = new char[] { '&' };

        public static string sysApplicationName = "Quan ly";
        public const bool sysEnterSameAsTabKey = true;
        public const char sysFindMarker = '?';
        public const string sysNewDataKeyMarker ="_";
        public const string sysNewEditKeyAutoMarker = "<AUTO>";
        public const string sysNewDataText = "<New>";

        //private static string _sysConfigFile = "sysconfig.xml";
        private static string _sysConfigFile = null;
        public static string sysConfigFile
        {
            get
            {
                if (_sysConfigFile == null) _sysConfigFile = fileFuncs.MakeFileNameFromExecutablePath(".xml");
                return _sysConfigFile;
            }
            set
            {
                _sysConfigFile = value;
            }
        }

        private static string _sysUserConfigFile = null;
        public static string sysUserConfigFile
        {
            get
            {
                if (_sysUserConfigFile == null)
                {

                    _sysUserConfigFile = fileFuncs.GetFullPath("user.xml");
                }
                return _sysUserConfigFile;
            }
            set
            {
                _sysUserConfigFile = value;
            }
        }


        //License
        public static string sysProductCode = "";
        public static string sysProductOwner = "";
        public static string sysProductVersion = "";

        //============================================================================================
        // Database connection information to ensure that all database access can be done
        //============================================================================================
        
        public static int sysDbTimeoutInSecs = 15;
        public static bool sysDbUseTransaction = false;

        private static myTypes.dbConnectionInfo[] _myDbConInfos = null;
        private static myTypes.wsConnectionInfo[] _myWsConInfos = null;

        public static myTypes.dbConnectionInfo myMainDbConInfo
        {
            get
            {
                return myDbConInfos[0];
            }
        }
        public static myTypes.dbConnectionInfo[] myDbConInfos
        {
            get
            {
                if (_myDbConInfos == null)
                {
                    LoadConnectionConfig();
                }
                return _myDbConInfos;
            }
        }
        public static myTypes.wsConnectionInfo[] myWsConInfos
        {
            get
            {
                if (_myWsConInfos == null)
                {
                    LoadConnectionConfig();
                }
                return _myWsConInfos;
            }
        }
        private static void LoadConnectionConfig()
        {
            _myWsConInfos = new myTypes.wsConnectionInfo[0];
            _myDbConInfos = new myTypes.dbConnectionInfo[0];
            int idx = 0;
            while (true)
            {
                myTypes.dbConnectionInfo dbInfo = configuration.GetDbConectionInfo(idx, true);
                if (dbInfo == null) break;
                Array.Resize(ref _myDbConInfos, _myDbConInfos.Length + 1);
                _myDbConInfos[_myDbConInfos.Length - 1] = dbInfo;
                idx++;
            }
            myTypes.wsConnectionInfo wsInfo = configuration.GetWebServiceInfo();
            if (wsInfo != null)
            {
                Array.Resize(ref _myWsConInfos, _myWsConInfos.Length + 1);
                _myWsConInfos[_myWsConInfos.Length - 1] = wsInfo;
            }
            if (_myDbConInfos == null || _myDbConInfos.Length < 1)
            {
                SysLibs.ShowErrorMessage(String.Format(language.GetString("configFileError"), fileFuncs.GetFullPath(Settings.sysConfigFile)));
                SysLibs.ExitApplication();
            }
        }
        public static void ClearConnectionConfig()
        {
            _myDbConInfos = null;
            _myWsConInfos = null;
        }
    }
}
